home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 8 code / Curves in Quickdraw / QD Curves / access demo.c next >
Encoding:
C/C++ Source or Header  |  1991-10-09  |  9.0 KB  |  400 lines  |  [TEXT/KAHL]

  1. #include <Memory.h>
  2. #include <ToolUtils.h>
  3. #include <Dialogs.h>
  4. #include <Resources.h>
  5. #include <Events.h>
  6. #include <Menus.h>
  7. #include <Fonts.h>
  8. #include <Scrap.h>
  9. #include <FixMath.h>
  10. #include <Desk.h>
  11. #include <OSEvents.h>
  12.  
  13. #include "access library.h"
  14. #include "access menu.h"
  15. #include "path2pict.h"
  16.  
  17. enum {
  18.     kAppleMenu = 1, kFileMenu, kEditMenu, kTypeMenu, kStyleMenu, kSpecialMenu
  19. };
  20.  
  21. enum {
  22.     kText = 1, kOutline = 3, kOutlinePoints
  23. };
  24.  
  25. WindowPtr gWind;
  26. static Rect stagRect = { 44, 8, 472, 632 };
  27. static Rect growRect = { 32, 32, 32000, 32000 };
  28. fontList gList;
  29. short gTypeItem = 0, gStyleItem = 0, gSpecialItem = kOutline;
  30. MenuHandle gTypeMenu, gStyleMenu, gSpecialMenu;
  31.  
  32. FixPoint gScale;
  33. short gPointSize;
  34. Str255 gName;
  35.  
  36. void InitToolbox()
  37. {
  38.     InitGraf(&qd.thePort); InitFonts(); FlushEvents(0xffff,0);
  39.     InitWindows(); InitMenus(); InitDialogs(0); TEInit(); InitCursor();
  40.  
  41.     gList = BuildFontList();
  42. }
  43.  
  44. void Pause(long ticks)
  45. {
  46.     long target = TickCount() + ticks;
  47.     while (TickCount() < target)
  48.         ;
  49. }
  50.  
  51. void Update(int changed)
  52. {
  53.     Rect r = qd.thePort->portRect;
  54.     short high, wide;
  55.  
  56.     EraseRect(&gWind->portRect);
  57.     InsetRect(&r, 16, 16);
  58.  
  59.     high = r.bottom - r.top;
  60.     wide = r.right - r.left;
  61.  
  62.     TextSize(120);
  63.     TextFont(GetFontFond(gList, gTypeItem, gStyleItem));
  64.     TextFace(GetFontFace(gList, gTypeItem, gStyleItem));
  65.  
  66.     SetOutlinePreferred(true);
  67.     SetPreserveGlyph(true);
  68.     SetFractEnable(true);
  69.  
  70.     {    Str255 name;
  71.         Handle sfnt = GetFontStyle(gList, gTypeItem, gStyleItem);
  72.         FontInfo info;
  73.  
  74.  
  75.         GetSfntNameString(sfnt, name_Family, name);
  76.         GetFontInfo(&info);
  77.         {    short width = StringWidth(name);
  78.             short height = info.ascent + info.descent;
  79.             Fixed scaler;
  80.  
  81.             if ( (long)high * width > (long)wide * height )
  82.                 scaler = FixRatio( wide, width );
  83.             else
  84.                 scaler = FixRatio( high, height );
  85.             TextSize(FixMul(qd.thePort->txSize, scaler));
  86.             GetFontInfo(&info);
  87.  
  88.             width = FixMul(scaler, width);
  89.             height = FixMul(scaler, height);
  90.             
  91.             width = r.right - r.left - width;
  92.             height = r.bottom - r.top - height;
  93.             MoveTo( r.left + width/2, r.top + height / 2 + info.ascent );
  94.             if (gSpecialItem == kText)
  95.                 DrawString(name);
  96.             else
  97.             {    FixPoint scale;
  98.  
  99.                 scale.y = scale.x = 0x10000;
  100.                 FrameText(name, &scale, gSpecialItem == kOutlinePoints);
  101.  
  102.                 gScale = scale;
  103.                 gPointSize = qd.thePort->txSize;
  104.                 BlockMove(name, gName, name[0] + 1);
  105.             }
  106.         }
  107.     }
  108.  
  109.     SetOutlinePreferred(false);
  110.     SetPreserveGlyph(false);
  111.     SetFractEnable(false);
  112.  
  113.     {    Str255 str;
  114.         Handle sfnt = GetFontStyle(gList, gTypeItem, gStyleItem);
  115.         TextFont(1);
  116.         TextFace(0);
  117.         TextSize(9);
  118.         MoveTo(12, 12); DrawString((ConstStr255Param)"\pFull: ");
  119.         GetSfntNameString(sfnt, name_FullName, str);
  120.         MoveTo(76, 12); DrawString(str);
  121.         MoveTo(12, 24); DrawString((ConstStr255Param)"\pUnique: ");
  122.         GetSfntNameString(sfnt, name_UniqueName, str);
  123.         MoveTo(76, 24); DrawString(str);
  124.         MoveTo(12, 36); DrawString((ConstStr255Param)"\pCopyright: ");
  125.         GetSfntNameString(sfnt, name_Copyright, str);
  126.         MoveTo(76, 36); DrawString(str);
  127.         MoveTo(12, 48); DrawString((ConstStr255Param)"\pTrademark: ");
  128.         GetSfntNameString(sfnt, name_Trademark, str);
  129.         MoveTo(76, 48); DrawString(str);
  130.         MoveTo(12, 60); DrawString((ConstStr255Param)"\pVersion: ");
  131.         GetSfntNameString(sfnt, name_Version, str);
  132.         MoveTo(76, 60); DrawString(str);
  133.     }
  134. }
  135.  
  136. void SetSpecial(short item)
  137. {
  138.     gSpecialItem = item;
  139.     
  140.     CheckItem(gSpecialMenu, kText, item == kText);
  141.     CheckItem(gSpecialMenu, kOutline, item == kOutline);
  142.     CheckItem(gSpecialMenu, kOutlinePoints, item == kOutlinePoints);
  143. }
  144.  
  145. int SetTypeStyle(short item)
  146. {
  147.     short count = CountFontStyles(gList, gTypeItem);
  148.     
  149.     if (item > count)
  150.         return false;
  151.  
  152.     gStyleItem = item;
  153.     {    short i, face = GetFontFace(gList, gTypeItem, item);
  154.         short faceItem = count + 2;
  155.  
  156.         for (i = 1; i <= count; i++)
  157.             CheckItem(gStyleMenu, i, i == item);
  158.         for (i = faceItem; i < faceItem + 7; i++)
  159.             CheckItem(gStyleMenu, i, false);
  160.         while (face)
  161.         {    if (face & 1)
  162.                 CheckItem(gStyleMenu, faceItem, true);
  163.             faceItem++;
  164.             face >>= 1;
  165.         }
  166.     }
  167.     return true;
  168. }
  169.  
  170. int SetTypeFace(short menuItem)
  171. {
  172.     {    Str255 name;
  173.         GetItem(gTypeMenu, menuItem, name);
  174.         gTypeItem = FindFamilyIndex(gList, name);
  175.         if (gTypeItem == 0)
  176.             gTypeItem = 1;
  177.     }
  178.  
  179.     {    short i, count = CountMItems(gTypeMenu);
  180.         for (i = 1; i <= count; i++)
  181.             CheckItem(gTypeMenu, i, menuItem == i);
  182.     }
  183.  
  184.     DeleteMenu(kStyleMenu);
  185.     DisposeMenu(gStyleMenu);
  186.     InsertMenu(gStyleMenu = NewMenu(kStyleMenu, (ConstStr255Param)"\pStyle"), kSpecialMenu);
  187.  
  188.     {    short i, count = CountFontStyles(gList, gTypeItem);
  189.         for (i = 1; i <= count; i++)
  190.             AppendMenu(gStyleMenu, (ConstStr255Param)"\pstyle");
  191.         AppendMenu(gStyleMenu, (ConstStr255Param)"\p(-;(Smear<B;(Slant<I;(Underline<U;(Outline<O;(Shadow<S;(Condense;(Extend");
  192.         SetItemStyle(gStyleMenu, count + 7, condense);
  193.         SetItemStyle(gStyleMenu, count + 8, extend);
  194.         
  195.         for (i = 1; i <= count; i++)
  196.         {    Str255 name;
  197.             Handle sfnt = GetFontStyle(gList, gTypeItem, i);
  198.             GetSfntNameString(sfnt, name_Subfamily, name);
  199.             SetItem(gStyleMenu, i, name);
  200.         }
  201.     }
  202.     SetTypeStyle(1);
  203.  
  204.     return true;
  205. }
  206.  
  207. void MakeTypeMenu(MenuHandle menu)
  208. {
  209.     short i, count = CountFontFamilies(gList);
  210.  
  211.     for (i = 1; i <= count; i++)
  212.     {    Str255 name;
  213.         Handle sfnt = GetFontStyle(gList, i, 1);
  214.         GetSfntNameString(sfnt, name_Family, name);
  215.         AppendMenu(menu, name);
  216.     }
  217.     SortMenu(menu);    /**/
  218.     SetTypeFace(1);
  219. }
  220.  
  221. void MakeMenus()
  222. {
  223.     char s[2];
  224.     MenuHandle aMenu;
  225.  
  226.     s[0] = 1; s[1] = appleMark;
  227.     InsertMenu(aMenu = NewMenu(kAppleMenu, (ConstStr255Param)s), 0);
  228.     AppendMenu(aMenu, (ConstStr255Param)"\pAbout last night…;(-");
  229.     AddResMenu(aMenu, 'DRVR');
  230.     InsertMenu(aMenu = NewMenu(kFileMenu, (ConstStr255Param)"\pFile"), 0);
  231.     AppendMenu(aMenu, (ConstStr255Param)"\pQuit/Q");
  232.     InsertMenu(aMenu = NewMenu(kEditMenu, (ConstStr255Param)"\pEdit"), 0);
  233.     AppendMenu(aMenu, (ConstStr255Param)"\p(Undo/Z;(-;(Cut/X;Copy/C;(Paste/V;(Clear");
  234.     InsertMenu(gTypeMenu = NewMenu(kTypeMenu, (ConstStr255Param)"\pType"), 0);
  235.     MakeTypeMenu(gTypeMenu);
  236.     InsertMenu(gStyleMenu = NewMenu(kStyleMenu, (ConstStr255Param)"\pStyle"), 0);
  237.     InsertMenu(gSpecialMenu = NewMenu(kSpecialMenu, (ConstStr255Param)"\pSpecial"), 0);
  238.     AppendMenu(gSpecialMenu, (ConstStr255Param)"\pText;(-;Outline;Outline+");
  239.     SetSpecial(gSpecialItem);
  240.     DrawMenuBar();
  241.     HiliteMenu(0);
  242. }
  243.  
  244. void CreatePict()
  245. {
  246.     Rect        myRect;
  247.     PicHandle    myPicHandle;
  248.     paths* text;
  249.     RgnHandle    saveClip = NewRgn();
  250.  
  251.     TextSize(gPointSize);
  252.     TextFont(GetFontFond(gList, gTypeItem, gStyleItem));
  253.     TextFace(GetFontFace(gList, gTypeItem, gStyleItem));
  254.  
  255.     text = Text2Paths(gName, &gScale, 0);
  256.     GetPathsBounds(text, &myRect);
  257.     myRect.right += qd.thePort->pnSize.h;
  258.     myRect.bottom += qd.thePort->pnSize.v;
  259.  
  260.     /*    Make room for the point handles
  261.     */
  262.     if (gSpecialItem == kOutlinePoints)
  263.         InsetRect(&myRect, -3, -3);
  264.  
  265.     GetClip( saveClip );
  266.  
  267.     myPicHandle = OpenPicture( &myRect );
  268.     ClipRect( &myRect );
  269.     if (gSpecialItem != kText )
  270.         AddPathsToPict( text );
  271.     if (gSpecialItem == kOutlinePoints)
  272.         MarkPaths( text );
  273.     ClosePicture();
  274.     
  275.     SetClip( saveClip );
  276.     DisposeRgn( saveClip );
  277.  
  278.     DisposePaths(text);
  279.  
  280.     ZeroScrap();
  281.     HLock( (Handle)myPicHandle );
  282.     PutScrap( GetHandleSize((Handle)myPicHandle), 'PICT', (Ptr)*myPicHandle );
  283.     HUnlock( (Handle)myPicHandle );
  284.     UnloadScrap();
  285.  
  286.     KillPicture( myPicHandle );
  287. }
  288.  
  289.  
  290. int DoMenu(long cmd)
  291. {
  292.     short menu = HiWord(cmd);
  293.     short item = LoWord(cmd);
  294.  
  295.     switch (menu)
  296.     {    case kFileMenu:
  297.             return true;
  298.         case kEditMenu:
  299.             if (item == 4)
  300.                 CreatePict();
  301.             break;
  302.         case kTypeMenu:
  303.             if (SetTypeFace(item))
  304.                 Update(true);
  305.             break;
  306.         case kStyleMenu:
  307.             if (SetTypeStyle(item))
  308.                 Update(true);
  309.             break;
  310.         case kSpecialMenu:
  311.             SetSpecial(item);
  312.             Update(true);
  313.             break;
  314.     }
  315.  
  316.     HiliteMenu(0);
  317.     return false;
  318. }
  319.  
  320. void DoMouse(Point p)
  321. {
  322. }
  323.  
  324.  
  325. int EventLoop()
  326. {
  327.     EventRecord    theEvent;
  328.     WindowPtr whichWindow;
  329.  
  330.     SystemTask();
  331.     if (!GetNextEvent(everyEvent, &theEvent))
  332.         return false;
  333.  
  334.     switch(theEvent.what)
  335.     {
  336.         case mouseDown:
  337.         switch (FindWindow(theEvent.where, &whichWindow))
  338.         {    case inSysWindow:
  339.                 SystemClick(&theEvent, whichWindow);
  340.                 break;
  341.             case inMenuBar:
  342.                 return DoMenu(MenuSelect(theEvent.where));
  343.                 break;
  344.             case inDrag:
  345.                 DragWindow(whichWindow, theEvent.where, &qd.screenBits.bounds);
  346.             break;
  347.             case inGoAway:
  348.                 if (TrackGoAway(whichWindow, theEvent.where))
  349.                     return true;
  350.             break;
  351.             case inContent:
  352.                 if (whichWindow != FrontWindow())
  353.                     SelectWindow(whichWindow);
  354.                 else if (whichWindow == gWind)
  355.                 {    Point p = theEvent.where;
  356.                 
  357.                     GlobalToLocal(&p);
  358.                     DoMouse(p);
  359.                 }
  360.             break;
  361.             case inGrow:
  362.             {    long newSize;
  363.                 DrawGrowIcon(whichWindow);
  364.                 newSize = GrowWindow(whichWindow, theEvent.where, &growRect);
  365.                 SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), true);
  366.                 SetPort(whichWindow);
  367.                 InvalRect(&whichWindow->portRect);
  368.             }
  369.             break;
  370.         }
  371.         break;
  372.         
  373.         case keyDown:
  374.             break;
  375.  
  376.         case updateEvt:
  377.             BeginUpdate((WindowPtr)theEvent.message);
  378.             Update(false);
  379.             EndUpdate((WindowPtr)theEvent.message);
  380.         break;
  381.     }
  382.     return false;
  383. }
  384.  
  385. main()
  386. {
  387.     long count, i;
  388.     Handle sfnt;
  389.     
  390.     InitToolbox();
  391.  
  392.     MakeMenus();
  393.  
  394.     SetPort(gWind = NewCWindow(0,&stagRect,(ConstStr255Param)"\pUntitled",true,0,(WindowPtr)-1,true,0));
  395.  
  396.     while (!EventLoop())
  397.         ;
  398. }
  399.  
  400.